package com.example.zhihuhelpapp.mvp;

import com.example.zhihuhelpapp.api.MySubscribe;
import com.example.zhihuhelpapp.api.RetrofitManager;
import com.example.zhihuhelpapp.api.mvp.RxPresenter;
import com.example.zhihuhelpapp.bean.AnswerResultBean;
import com.example.zhihuhelpapp.bean.AuthorBean;
import com.example.zhihuhelpapp.bean.QuestionBean;
import com.example.zhihuhelpapp.bean.QuestionResultBean;
import com.example.zhihuhelpapp.mvp.contract.IQuestionListContract;
import com.example.zhihuhelpapp.utils.CoreConstant;
import com.example.zhihuhelpapp.utils.DbUtils;
import com.example.zhihuhelpapp.utils.LogUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class QuestionListPresenter extends RxPresenter<IQuestionListContract.IView>
        implements IQuestionListContract.IPresenter {
    @Override
    public void saveQuestionToLocal(QuestionBean questionBean) {
        DbUtils.saveQuestion(questionBean);
        mView.onSaveQuestionSuccess();
    }

    @Override
    public void delLocalQuestion(String questionId) {
        DbUtils.delQuestion(questionId);
        mView.onDelQuestionSuccess();
    }

    @Override
    public void getLocalQuestionList() {
        mView.onGetLocalQuestionList(DbUtils.getQuestionBean());
    }

    @Override
    public void getQuestionDetail(String questionId, int page) {
        RetrofitManager.getInstance()
                .apiService()
                .getQuestionDetail(questionId, CoreConstant.QUESTION_PAGE_COUNT, CoreConstant.QUESTION_PAGE_COUNT * page)
                .compose(mView.bind())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MySubscribe<QuestionResultBean>() {
                    @Override
                    public void onNext(QuestionResultBean resultBean) {
                        if (resultBean != null) {
                            LogUtils.i("获取问题详情成功：" + resultBean.getPaging().getTotals());
                            mView.onGetQuestionDetail(resultBean);
                        } else {
                            LogUtils.i("获取问题详情失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LogUtils.i("获取问题详情异常：" + e.getMessage());
                    }
                });
    }

    @Override
    public void getAnswerDetail(final String answerId) {
        LogUtils.i("回答链接："+answerId);
        RetrofitManager.getInstance()
                .apiService()
                .getAnswerDetail(answerId)
                .compose(mView.bind())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MySubscribe<AnswerResultBean>() {
                    @Override
                    public void onNext(AnswerResultBean resultBean) {
                        if (resultBean != null) {
                            AuthorBean author = resultBean.getAuthor();
                            LogUtils.i("获取回答详情成功：" + author.getName());
                            AnswerResultBean.QuestionBean question = resultBean.getQuestion();
                            getQuestionDetail(String.valueOf(question.getId()),0);
                        } else {
                            LogUtils.i("获取回答详情失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LogUtils.i("获取回答详情异常：" + e.getMessage());
                    }
                });
    }
}
